/*
 * =====================================================================================
 *
 *       Filename:  mutex1.c
 *
 *    Description:  simple mutex
 *
 *        Version:  1.0
 *        Created:  12-04-24 10:52:57 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

void *functionC(void *pparam);
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int counter = 0;

int main(){
	int rc1, rc2;
	pthread_t thread1, thread2;

	int i = 1;
	int j = 2;

	rc1=pthread_create(&thread1, NULL, &functionC, &i);
	if(rc1){
		printf("Thread creation failed: %d\n", rc1);
	}

	if ((rc2=pthread_create(&thread2, NULL, &functionC, &j))){
		printf("Thread creation failed: %d\n", rc2);
	}

	pthread_join(thread1, NULL);
	pthread_join(thread2, NULL);

	return 0;
}

void *functionC(void *pparam){

	while(1){
		pthread_mutex_lock(&mutex1);
		counter++;
		printf("Thread %d Counter value: %d\n", *((int *)pparam), counter);
		pthread_mutex_unlock(&mutex1);
	}
}
